<!DOCTYPE html>
<html>
	<head>
		<title>设置个人资料</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<script src="/js/bootstrap/js/bootstrap.min.js"></script>
		<script src="/js/plugins/jquery-form/jquery.form.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/myInfo.css" />
		<script src="/js/plugins/jrender/jrender.min.js"></script>



		<script>
            $(function () {
                //回显个人信息页面
                var userStr = sessionStorage.getItem("user");
                var user = JSON.parse(userStr);
                $(".info").renderValues(user);

                //点击<div class="myBg">样式,发现背景图片写死
				//设置背景墙,添加样式
				$(".myBg").css("background-image","url('"+user.coverImgUrl+"')")

				//由于相同的操作,所以可以提取一个公共的变量
				var flag;

				//提交表单
				$("#headBtn").click(function () {
				    //不带回调函数的直接调用绑定在点击事件里面的函数
					$("#file").click();
					flag=1;
                })
                //上传背景图
                $("#coverBtn").click(function () {
                    $("#file").click();
                    flag=0;
                })



				//通过选择的值改变事件,判断是否已经选择了图片
				$("#file").change(function () {
					var value =this.value;
					if(value){
					    //图片上传的操作
						$("#fileForm").ajaxSubmit(function (data) {
							if(data.status==1){
							    if(flag==1){
									$("#headImg").attr("src",data.url)
									$("#headImgUrl").val(data.url)
								}else{
                                    $(".myBg").css("background-image","url('"+data.url+"')")
									$("#coverImgUrl").val(data.url)
								}
							}
                        })
					}
                })

				$("#saveBtn").click(function () {
					$("#infoForm").ajaxSubmit(function (data) {
					    if(data.success){
					        //将值设置到缓存中
							sessionStorage.setItem("user",JSON.stringify(data.obj));
							window.location.href="/mine/profiles.html";
						}
                    })
                })

            })



		</script>
		
	</head>

	<body>

	<!--设置隐藏的表单-->
	<form action="/images" method="post" enctype="multipart/form-data" id="fileForm">
		<!--name属性要和方法中接收的参数名一致,否则无法封装-->
		<!--style="display: none"表示隐藏文件选择框-->
		<input type="file" name="multipartFile" style="display: none" id="file">
	</form>
		<div class="search-head">
			<div class="row nav-search">
				<div class="col">
					<a href="javascript:window.history.go(-1);">
						<span>取消</span>
					</a>
				</div>
				<div class="col">
					<span>设置个人资料</span>
				</div>
				<div class="col" >
					<a href="javascript:" id="saveBtn" >
						<span>保存</span>
					</a>
				</div>
			</div>
		</div>

		<div class="container info">
			<div class="myBg">
				<img id="headImg"  class="rounded-circle " render-src="headImgUrl"/>
				<div class="editHead" id="headBtn" ><i class="fa fa-pencil-square fa-lg " aria-hidden="true"></i></div>
				<button  class="btn btn-outline-light ibtn " id="coverBtn">设置背景</button>
			</div>

			<form id="infoForm" action="/users" method="post" >
					<input type="hidden" name="_method" value="put">
					<input type="hidden" name="id" render-value="id">
					<input type="hidden" name="headImgUrl" id="headImgUrl" render-value="headImgUrl">
					<input type="hidden" name="coverImgUrl" id="coverImgUrl" render-value="coverImgUrl">

					<label>昵称</label><input type="text" name="nickName" class="form-control" render-value="nickName">
					<label>性别</label><select class="form-control" name="gender" render-value="gender" >
							<option value="-1">保密</option>
							<option value="0">男</option>
							<option value="1">女</option>
						</select>
					<label>地区</label><input type="text" name="place"  class="form-control" render-value="place">
					<label>签名</label><textarea name="sign" class="form-control" rows="5" render-value="sign"></textarea>
			</form>
		</div>
		
		
		
	</body>
</html>
